package com.cy.store.controller;

import com.cy.store.controller.ex.*;
import com.cy.store.service.ex.*;
import com.cy.store.util.JsonResult;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpSession;

/**
 * @author: AngJie
 * @create: 2022-02-06 01:11
 **/
// 控制层类的基类
public class BaseController {
    public static final int OK = 200;

    // 请求处理方法，这个方法的返回值就是需要传递给前端的数据
    // 自动将异常对象传递给此方法的参数列表上
    // 当前项目中产生了异常，被统一拦截到此方法中，这个方法就充当的是处理请求方法,方法返回值发送给前端
    @ExceptionHandler({ServiceException.class, FileUploadException.class}) //用于统一处理抛出的异常
    public JsonResult handleException(Throwable e) {
        JsonResult result = new JsonResult();
        if (e instanceof UsernameDuplicatedException) {
            result.setState(4000);
            result.setMessage(e.getMessage());
        } else if (e instanceof UserNotFonudException) {
            result.setState(4001);
            result.setMessage(e.getMessage());
        } else if (e instanceof PasswordNotMatchException) {
            result.setState(4002);
            result.setMessage(e.getMessage());
        } else if (e instanceof LoggedOffException) {
            result.setState(4003);
            result.setMessage(e.getMessage());
        } else if (e instanceof PasswordDuplicatedException) {
            result.setState(4004);
            result.setMessage(e.getMessage());
        } else if (e instanceof AddressCountLimitException) {
            result.setState(4005);
            result.setMessage(e.getMessage());
        } else if (e instanceof AddressNotFoundException) {
            result.setState(4006);
            result.setMessage(e.getMessage());
        } else if (e instanceof AccessDeniedException) {
            result.setState(4007);
            result.setMessage(e.getMessage());
        } else if (e instanceof ProductNotFoundException) {
            result.setState(4008);
            result.setMessage(e.getMessage());
        } else if (e instanceof CartNotFoundException) {
            result.setState(4009);
            result.setMessage(e.getMessage());
        } else if (e instanceof InsertException) {
            result.setState(5000);
            result.setMessage(e.getMessage());
        } else if (e instanceof UpdateException) {
            result.setState(5001);
            result.setMessage(e.getMessage());
        } else if (e instanceof DeleteException) {
            result.setState(5002);
            result.setMessage(e.getMessage());
        } else if (e instanceof FileEmptyException) {
            result.setState(6000);
            result.setMessage(e.getMessage());
        } else if (e instanceof FileSizexception) {
            result.setState(6001);
            result.setMessage(e.getMessage());
        } else if (e instanceof FileTypeException) {
            result.setState(6002);
            result.setMessage(e.getMessage());
        } else if (e instanceof FileStateException) {
            result.setState(6003);
            result.setMessage(e.getMessage());
        } else if (e instanceof FileUploadIOException) {
            result.setState(6004);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    /**
     * 获取session对象中的uid
     *
     * @param session
     * @return [当前登录用户的uid]
     * @author AngJie
     * @creed: Talk is cheap,show me the code
     * @date 2022/2/6 0006 17:31
     */
    protected final Integer getUidFromSession(HttpSession session) {
        return Integer.valueOf(session.getAttribute("uid").toString());
    }

    /**
     * @param session
     * @return [当前登录用户的用户名称]
     * @author AngJie
     * @creed: Talk is cheap,show me the code
     * @date 2022/2/6 0006 17:36
     */
    protected final String getUsernameFromSession(HttpSession session) {
        // session.getAttribute("var") 返回的是Object类型，toString()并未重写
        // 但是HttpSession本身是接口，传入的session的HttpSession的实现类对象
        return session.getAttribute("username").toString();
    }
}
